library(gganimate)
library(ggplot2)
library(maps)
library(ggthemes)
library(ggspatial)


###############################
###Disease outbreaks by type###
###############################
##Set WD
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
####Read the disease data
dis.dat <- read.csv("ZootonicDat_2_23D--WD ProMedWHO robustness.csv")
summary(dis.dat)
#Remove the empty rows
dis.dat <- dis.dat[!is.na(dis.dat$COWCODE),]
#Keep only subnational data
dis.dat <- subset(dis.dat, SpatialPrec<4)
#Create an outbreak indicator
dis.dat$outbreak <- 1
summary(dis.dat)

#Collapse by lat and long
out.dat.col <- summaryBy(outbreak~Latitude+Longitude, data=dis.dat, FUN=c("sum"), keep.names = T)
out.dat.col.nz <- subset(out.dat.col,outbreak>0 )
table(out.dat.col.nz$outbreak)

##sort
out.dat.col.nz <- out.dat.col.nz[with(out.dat.col.nz, order(outbreak)), ]
#Create a class indicator
out.dat.col.nz$classe <- ifelse(out.dat.col.nz$outbreak <= 5,"01-5",
                                 ifelse((out.dat.col.nz$outbreak > 5 & out.dat.col.nz$outbreak <= 10), "05-10",
                                        ifelse((out.dat.col.nz$outbreak > 10 & out.dat.col.nz$outbreak <= 15), "10-15",
                                               ifelse((out.dat.col.nz$outbreak > 15 & out.dat.col.nz$outbreak <= 20),"15-20",
                                                      ifelse((out.dat.col.nz$outbreak > 20 & out.dat.col.nz$outbreak <= 30),"20-30",
                                                                    "More than 30")))))
unique(out.dat.col.nz$classe)  
#Color scale
cc <- scales::seq_gradient_pal("#FF00FF", "#551A8B", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz$classe))))

african.countries <- c(
  "Algeria","Angola","Benin","Botswana","Burkina Faso","Burundi","Cabo Verde",
  "Cameroon","Central African Republic","Chad","Comoros","Democratic Republic of the Congo",
  "Republic of Congo","Ivory Coast","Djibouti","Egypt","Equatorial Guinea",
  "Eritrea","Eswatini","Ethiopia","Gabon","Gambia","Ghana","Guinea","Guinea-Bissau",
  "Kenya","Lesotho","Liberia","Libya","Madagascar","Malawi","Mali","Mauritania","Mauritius",
  "Morocco","Mozambique","Namibia","Niger","Nigeria","Rwanda","Sao Tome and Principe",
  "Senegal","Seychelles","Sierra Leone","Somalia","South Africa","South Sudan",
  "Sudan","Tanzania","Togo","Tunisia","Uganda","Zambia","Zimbabwe", "Swaziland", "Western Sahara"
)


##############
###Plot map###
##############
#Begin with a map of the African states
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

##Create the main map
map.all.col <- world +
  geom_point(aes(x = Longitude, y = Latitude, color = classe ),
             data = out.dat.col.nz, alpha = .6) +
  scale_colour_manual(name="Outbreaks", values=cc)+
#North star
ggspatial::annotation_north_arrow(
  location = "ttr", #which_north = "true",
  pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
  style = ggspatial::north_arrow_nautical(
    fill = c("grey40", "white"),
    line_col = "grey20"
  )
)+
  geom_text(aes(x = 48.5, y = -50.5, 
                label = "Scale 1:50,000,000"),size=3,fontface='bold')

##Export map
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
jpeg("outbreakcat19962019tot.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col
dev.off()



###############################
###############################
###Conflict events by type###
###############################
###############################

#####################
###State Conflicts###
#####################
out.dat2 <- full.1996.2019[,c("latitude","longitude","acled_battle_state", "ym","t", "month")]
#Collapse by lat and long
out.dat.col2 <- summaryBy(acled_battle_state~latitude+longitude, data=out.dat2, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz2 <- subset(out.dat.col2,acled_battle_state>0 )
table(out.dat.col.nz2$acled_battle_state)

##sort
out.dat.col.nz2 <- out.dat.col.nz2[with(out.dat.col.nz2, order(acled_battle_state)), ]

#Create a discrete conflict cateogry
out.dat.col.nz2$classe <- ifelse(out.dat.col.nz2$acled_battle_state <= 10,"1-10",
                                 ifelse((out.dat.col.nz2$acled_battle_state > 10 & out.dat.col.nz2$acled_battle_state <= 20), "10-20",
                                        ifelse((out.dat.col.nz2$acled_battle_state > 20 & out.dat.col.nz2$acled_battle_state <= 30), "20-30",
                                               ifelse((out.dat.col.nz2$acled_battle_state > 30 & out.dat.col.nz2$acled_battle_state <= 40), "30-40",
                                                      ifelse((out.dat.col.nz2$acled_battle_state > 40 & out.dat.col.nz2$acled_battle_state <= 50), "40-50",
                                                             ifelse((out.dat.col.nz2$acled_battle_state > 50 & out.dat.col.nz2$acled_battle_state <= 75), "50-75",
                                                                    ifelse((out.dat.col.nz2$acled_battle_state > 75 & out.dat.col.nz2$acled_battle_state <= 99), "75-99",
                                                                           "More than 99")))))))

unique(out.dat.col.nz2$classe)

#Color scale
cc <- scales::seq_gradient_pal("#FF7276", "black", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz2$classe))))

##Create world map framework
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col2 <- world +
  geom_point(aes(x = longitude, y = latitude, color = classe),
             data = out.dat.col.nz2, alpha=0.8) +
  scale_colour_manual(name="State conflicts", values=cc)+
  #North star
  ggspatial::annotation_north_arrow(
    location = "ttr", #which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20"
    )
  )+
  geom_text(aes(x = 48.5, y = -50.5, 
                label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
jpeg("stateconfsum19972019.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col2
dev.off()


#####################
###Rebel Conflicts###
#####################
out.dat3 <- full.1996.2019[,c("latitude","longitude","acled_battle_rebel", "ym","t", "month")]
#Collapse by lat and long
out.dat.col3 <- summaryBy(acled_battle_rebel~latitude+longitude, data=out.dat3, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz3 <- subset(out.dat.col3,acled_battle_rebel>0 )
summary(out.dat.col.nz3$acled_battle_rebel)

##sort
out.dat.col.nz3 <- out.dat.col.nz3[with(out.dat.col.nz3, order(acled_battle_rebel)), ]

#Create a discrete conflict cateogry
out.dat.col.nz3$classe <- ifelse(out.dat.col.nz3$acled_battle_rebel <= 10,"1-10",
                                 ifelse((out.dat.col.nz3$acled_battle_rebel > 10 & out.dat.col.nz3$acled_battle_rebel <= 20), "10-20",
                                        ifelse((out.dat.col.nz3$acled_battle_rebel > 20 & out.dat.col.nz3$acled_battle_rebel <= 30), "20-30",
                                               ifelse((out.dat.col.nz3$acled_battle_rebel > 30 & out.dat.col.nz3$acled_battle_rebel <= 40), "30-40",
                                                      ifelse((out.dat.col.nz3$acled_battle_rebel > 40 & out.dat.col.nz3$acled_battle_rebel <= 50), "40-50",
                                                             ifelse((out.dat.col.nz3$acled_battle_rebel > 50 & out.dat.col.nz3$acled_battle_rebel <= 100), "50-100",
                                               "More than 100"))))))

unique(out.dat.col.nz3$classe)

#Color scale
cc <- scales::seq_gradient_pal("#FF7276", "black", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz3$classe))))


##Create world map framework
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col3 <- world +
  geom_point(aes(x = longitude, y = latitude, color = classe),
             data = out.dat.col.nz3, alpha = .8) +
  scale_colour_manual(name="Rebel conflicts", values=cc)+
  #North star
  ggspatial::annotation_north_arrow(
    location = "ttr", #which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20"
    )
  )+
  geom_text(aes(x = 48.5, y = -50.5, 
                label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
jpeg("rebelconfsum19972019.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col3
dev.off()


######################
###PolMil Conflicts###
######################
out.dat4 <- full.1996.2019[,c("latitude","longitude","acled_battle_polmil", "ym","t", "month")]
#Collapse by lat and long
out.dat.col4 <- summaryBy(acled_battle_polmil~latitude+longitude, data=out.dat4, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz4 <- subset(out.dat.col4,acled_battle_polmil>0 )
table(out.dat.col.nz4$acled_battle_polmil)

##sort
out.dat.col.nz4 <- out.dat.col.nz4[with(out.dat.col.nz4, order(acled_battle_polmil)), ]

#Create a discrete conflict cateogry
out.dat.col.nz4$classe <- ifelse(out.dat.col.nz4$acled_battle_polmil <= 10,"1-10",
                                 ifelse((out.dat.col.nz4$acled_battle_polmil > 10 & out.dat.col.nz4$acled_battle_polmil <= 20), "10-20",
                                        ifelse((out.dat.col.nz4$acled_battle_polmil > 20 & out.dat.col.nz4$acled_battle_polmil <= 30), "20-30",
                                               ifelse((out.dat.col.nz4$acled_battle_polmil > 30 & out.dat.col.nz4$acled_battle_polmil <= 40), "30-40",
                                                      ifelse((out.dat.col.nz4$acled_battle_polmil > 40 & out.dat.col.nz4$acled_battle_polmil <= 50), "40-50",
                                                             ifelse((out.dat.col.nz4$acled_battle_polmil > 50 & out.dat.col.nz4$acled_battle_polmil <= 100), "50-100",
                                                                    "More than 100"))))))

unique(out.dat.col.nz4$classe)

#Color scale
cc <- scales::seq_gradient_pal("#FF7276", "black", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz4$classe))))


##Create world map framework
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col4 <- world +
  geom_point(aes(x = longitude, y = latitude, color = classe),
             data = out.dat.col.nz4, alpha = .8) +
  scale_colour_manual(name="Political militia conflicts", values=cc)+
  #North star
  ggspatial::annotation_north_arrow(
    location = "ttr", #which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20"
    )
  )+
  geom_text(aes(x = 48.5, y = -50.5, 
                label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
jpeg("polmilconfsum19972019.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col4
dev.off()

######################
###IdMil Conflicts###
######################
out.dat5 <- full.1996.2019[,c("latitude","longitude","acled_battle_idmil", "ym","t", "month")]
#Collapse by lat and long
out.dat.col5 <- summaryBy(acled_battle_idmil~latitude+longitude, data=out.dat5, FUN=c("sum"),keep.names = T, na.rm=T)
out.dat.col.nz5 <- subset(out.dat.col5,acled_battle_idmil>0 )
table(out.dat.col.nz5$acled_battle_idmil)

##sort
out.dat.col.nz5 <- out.dat.col.nz5[with(out.dat.col.nz5, order(acled_battle_idmil)), ]

#Create a discrete conflict cateogry
out.dat.col.nz5$classe <- ifelse(out.dat.col.nz5$acled_battle_idmil <= 10,"1-10",
                                 ifelse((out.dat.col.nz5$acled_battle_idmil > 10 & out.dat.col.nz5$acled_battle_idmil <= 20), "10-20",
                                        ifelse((out.dat.col.nz5$acled_battle_idmil > 20 & out.dat.col.nz5$acled_battle_idmil <= 30), "20-30",
                                               ifelse((out.dat.col.nz5$acled_battle_idmil > 30 & out.dat.col.nz5$acled_battle_idmil <= 40), "30-40",
                                                      ifelse((out.dat.col.nz5$acled_battle_idmil > 40 & out.dat.col.nz5$acled_battle_idmil <= 50), "40-50",
                                                             "More than 50")))))

unique(out.dat.col.nz5$classe)

#Color scale
cc <- scales::seq_gradient_pal("#FF7276", "black", "Lab")(seq(0,1,length.out=length(unique(out.dat.col.nz5$classe))))


##Create world map framework
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Plot
map.all.col5 <- world +
  geom_point(aes(x = longitude, y = latitude, color = classe),
             data = out.dat.col.nz5, alpha = .8) +
  scale_colour_manual(name="Identity militia conflicts", values=cc)+
  #North star
  ggspatial::annotation_north_arrow(
    location = "ttr", #which_north = "true",
    pad_x = unit(0.4, "in"), pad_y = unit(0.4, "in"),
    style = ggspatial::north_arrow_nautical(
      fill = c("grey40", "white"),
      line_col = "grey20"
    )
  )+
  geom_text(aes(x = 48.5, y = -50.5, 
                label = "Scale 1:100,000,000"),size=3,fontface='bold')

##Export map
setwd("~/OneDrive - Indiana University/FromGoogle/GeolocatedDisease/")
jpeg("idmilconfsum19972019.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col5
dev.off()

